import os
from docx import Document
import matplotlib.pyplot as plt

"""
@FileName：职业成熟度.py
@Description：

@Author：HeYiQing
@Time：2024/5/14 16:30
"""


def get_name(tables):
    # 个人基本信息
    info_table = tables[0]
    name = info_table.cell(0, 3).text
    return name


def subplots(data, title):
    # 正确显示负号
    plt.rcParams['axes.unicode_minus'] = False
    # 使用黑体字体显示中文
    plt.rcParams['font.sans-serif'] = ['SimHei']
    labels = ['较成熟的', '欠成熟的']
    count_1 = data.count(1)
    count_2 = len(data) - count_1
    sizes = [count_1, count_2]
    colors = ['#99FF99', '#FF9999']
    explode = (0.1, 0)
    fig1, ax1 = plt.subplots()
    ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', colors=colors)
    ax1.axis('equal')
    ax1.set_title(title)
    path = "F:/比赛/数学建模/24/repo/职业成熟度/结果/饼图/" + title + ".png"
    directory = os.path.dirname(path)
    if not os.path.exists(directory):
        os.makedirs(directory)
    plt.savefig(path)
    plt.show()


# 绘制折线图
def plot(x, y, title, x_label, y_label):
    y_norm = ['信息应用', '职业认知', '自我认知', '个人调适', '职业态度', '价值观念', '职业选择', '条件评估']
    # 正确显示负号
    plt.rcParams['axes.unicode_minus'] = False
    # 使用黑体字体显示中文
    plt.rcParams['font.sans-serif'] = ['SimHei']
    # 创建图表和轴
    plt.figure(figsize=(30, 24))
    # 为了在x轴上使用字符串，我们需要一个索引列表
    indices = range(len(x))
    # 绘制折线图
    plt.plot(x, y, marker='o')
    # 设置x轴的标签为姓名
    plt.xticks(indices, x, rotation=45)
    # 添加标题和轴标签
    plt.title(title, fontsize=24)
    plt.xlabel(x_label, fontsize=24)
    plt.ylabel(y_label, fontsize=24)
    # 添加图例
    plt.legend(labels=y_norm)
    # 显示图形
    plt.grid(True)
    # 保存文件
    path = "F:/比赛/数学建模/24/repo/职业成熟度/结果/折线图/" + title + ".png"
    directory = os.path.dirname(path)
    if not os.path.exists(directory):
        os.makedirs(directory)
    plt.savefig(path)
    # 显示图表
    plt.show()
    # 关闭
    plt.close()


def get_test_data(tables):
    data_table = tables[1]
    score_list = []
    avg_list = []
    for item in range(1, 10):
        try:
            score_list.append(int(data_table.cell(item, 1).text))
        except Exception:
            score_list.append(0)
    for item in range(1, 10):
        try:
            avg_list.append(float(data_table.cell(item, 2).text))
        except Exception:
            avg_list.append(0.00)
    res_list = []
    res_list.append(score_list)
    res_list.append(avg_list)
    return res_list


# 获取姓名
name_list_A = []
name_list_B = []


def table_op(STR, file_dir_A, file_dir_B, file_list_A, file_list_B):
    # 获取职业成熟度表
    file_table_A = []
    file_table_B = []
    for file_name in file_list_A:  #
        if STR in file_name:
            table_list = get_tables(file_dir_A, file_name)  #
            file_table_A.append(table_list)
    for file_name in file_list_B:  #
        if STR in file_name:
            table_list = get_tables(file_dir_B, file_name)  #
            file_table_B.append(table_list)
    for tables in file_table_A:
        name_list_A.append(get_name(tables))
    for tables in file_table_B:
        name_list_B.append(get_name(tables))

    A_list = []
    B_list = []
    A_B_list = []

    for tables in file_table_A:
        A_list.append(get_test_data(tables))
    for tables in file_table_B:
        B_list.append(get_test_data(tables))
    A_B_list = A_list + B_list
    A_B_name = name_list_A + name_list_B

    score_list_A = []
    avg_list_A = []
    score_list_B = []
    avg_list_B = []
    score_list = []
    avg_list = []

    for res in A_list:
        score_list_A.append(res[0])
        avg_list_A.append(res[1])
    for res in B_list:
        score_list_B.append(res[0])
        avg_list_B.append(res[1])
    score_list = score_list_A + score_list_B
    avg_list = avg_list_A + avg_list_B
    plot(name_list_A, score_list_A, "职业成熟度-A-原始分-姓名", "姓名", "原始分")
    plot(name_list_B, score_list_B, "职业成熟度-B-原始分-姓名", "姓名", "原始分")
    plot(A_B_name, score_list, "职业成熟度-A-B-原始分-姓名", "姓名", "原始分")
    plot(name_list_A, avg_list_A, "职业成熟度-A-均分-姓名", "姓名", "均分")
    plot(name_list_B, avg_list_B, "职业成熟度-B-均分-姓名", "姓名", "均分")
    plot(A_B_name, avg_list, "职业成熟度-A-B-均分-姓名", "姓名", "均分")


# 获取文件中所有表格
def get_tables(path, file_name):
    abs_file_path = os.path.join(path, file_name)
    file = Document(abs_file_path)
    tables = file.tables
    return tables


def get_paragraphs(path, file_name):
    abs_file_path = os.path.join(path, file_name)
    file = Document(abs_file_path)
    paragraphs = file.paragraphs
    res = paragraphs[14:31][::2]
    result_list = []
    for i, run in enumerate(res):
        if i == 0:
            if run.text[-5:-1] == '较成熟的':
                result_list.append(1)
            else:
                result_list.append(0)
        else:
            if run.text[-5:-1] == '较为成熟':
                result_list.append(1)
            else:
                result_list.append(0)

    return result_list


def paragraphs_op(STR, file_dir_A, file_dir_B, file_list_A, file_list_B):
    # 获取大五人格表
    file_table_A = []
    file_table_B = []
    file_paragraphs_A = []
    file_paragraphs_B = []
    for file_name in file_list_A:  #
        if STR in file_name:
            tables = get_tables(file_dir_A, file_name)
            file_table_A.append(tables)
            paragraphs_list = get_paragraphs(file_dir_A, file_name)  #
            file_paragraphs_A.append(paragraphs_list)
    for file_name in file_list_B:  #
        if STR in file_name:
            tables = get_tables(file_dir_B, file_name)
            file_table_B.append(tables)
            paragraphs_list = get_paragraphs(file_dir_B, file_name)  #
            file_paragraphs_B.append(paragraphs_list)
    for tables in file_table_A:
        name_list_A.append(get_name(tables))
    for tables in file_table_B:
        name_list_B.append(get_name(tables))
    tester_A = []
    tester_B = []
    tester = []
    res_A = []
    res_B = []
    res = []
    for item in file_paragraphs_A:
        res_A.append(item[0])
        tester_A.append(item[1:])
    for item in file_paragraphs_B:
        res_B.append(item[0])
        tester_B.append(item[1:])
    tester = tester_A + tester_B
    res = res_A + res_B
    name_list = name_list_A + name_list_B
    # plot(name_list_A, tester_A, '大五人格-A-项目-姓名', '姓名', '测试结果')
    # plot(name_list_B, tester_B, '大五人格-B-项目-姓名', '姓名', '测试结果')
    # plot(name_list, tester, '大五人格-A-B-项目-姓名', '姓名', '测试结果')
    # plot(name_list_A, res_A, '大五人格-A-结果-姓名', '姓名', '测试结果')
    # plot(name_list_B, res_B, '大五人格-B-结果-姓名', '姓名', '测试结果')
    # plot(name_list, res, '大五人格-A-B-结果-姓名', '姓名', '测试结果')
    # subplots(res_A, '大五人格-A-结果-姓名')
    # subplots(res_B, '大五人格-B-结果-姓名')
    # subplots(res, '大五人格-A-B-结果-姓名')
    warn_list = []
    for i, item in enumerate(res):
        if item == 0:
            warn_list.append(name_list[i])
    print(warn_list)


if __name__ == '__main__':
    STR = "职业成熟度"
    file_dir_A = r"E:\code_special\Gitee\Math_C\repo\A组--69名测试者"
    file_dir_B = r"E:\code_special\Gitee\Math_C\repo\B组--79名测试者"
    file_list_A = os.listdir(file_dir_A)
    file_list_B = os.listdir(file_dir_B)
    # table_op(STR, file_dir_A, file_dir_B, file_list_A, file_list_B)
    # get_paragraphs(file_dir_A, 'A003---职业成熟度量表.docx')
    paragraphs_op(STR, file_dir_A, file_dir_B, file_list_A, file_list_B)
